package com.base.server.search.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.base.server.search.pojo.SearchModel;
import com.base.server.search.service.SearchService;
import com.monitor.api.ApiInfo;
import com.monitor.api.ApiParam;
import com.monitor.api.ApiRes;
import com.system.comm.controller.BaseRestController;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * 搜索的Controller
 * @author autoCode
 * @date 2019-07-13 16:27:05
 * @version V1.0.0
 */
@RestController
public class SearchController extends BaseRestController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SearchController.class);
	
	@Autowired
	private SearchService searchService;

	@RequestMapping(name = "搜索-创建索引", value = "/search/createIndex")
	@ApiInfo(params = {
			@ApiParam(name="编号", code="id", value=""),
			@ApiParam(name="标题", code="title", value=""),
			@ApiParam(name="内容", code="content", value=""),
			@ApiParam(name="来源系统[jingya惊讶、iae等]", code="system", value=""),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
	})
	public ResponseFrame createIndex(SearchModel searchModel, String system) {
		try {
			ResponseFrame frame = searchService.createIndex(searchModel, system);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}
	
	@RequestMapping(name = "搜索-更新索引", value = "/search/updateIndex")
	@ApiInfo(params = {
			@ApiParam(name="编号", code="id", value=""),
			@ApiParam(name="标题", code="title", value=""),
			@ApiParam(name="内容", code="content", value=""),
			@ApiParam(name="来源系统[jingya惊讶、iae等]", code="system", value=""),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
	})
	public ResponseFrame updateIndex(SearchModel searchModel, String system) {
		try {
			ResponseFrame frame = searchService.updateIndex(searchModel, system);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}
	@RequestMapping(name = "搜索-删除索引", value = "/search/deleteIndex")
	@ApiInfo(params = {
			@ApiParam(name="编号", code="id", value=""),
			@ApiParam(name="来源系统[jingya惊讶、iae等]", code="system", value=""),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
	})
	public ResponseFrame deleteIndex(SearchModel searchModel, String system) {
		try {
			ResponseFrame frame = searchService.deleteIndex(searchModel, system);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}
	@RequestMapping(name = "搜索-创建索引", value = "/search/search")
	@ApiInfo(params = {
			@ApiParam(name="搜索字符串[多个,分隔]", code="searchString", value=""),
			@ApiParam(name="搜索字段[多个,分隔]", code="searchField", value=""),
			@ApiParam(name="来源系统[jingya惊讶、iae等]", code="system", value=""),
	}, response = {
			@ApiRes(name="响应码[0成功、-1失败]", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
	})
	public ResponseFrame search(String searchString, String searchField, String system) {
		try {
			ResponseFrame frame = searchService.search(searchString, searchField, system);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}
}